package com.uu.safeguard.controller;

import com.uu.safeguard.result.Result;
import com.uu.safeguard.result.ResultFactory;
import com.uu.safeguard.service.MemberRechargeLogService;
import com.uu.safeguard.service.MemberService;
import com.uu.safeguard.utils.CommonUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/memberRechargeLog")
public class MemberRechargeLogController {
    private Logger logger = LoggerFactory.getLogger(MemberRechargeLogController.class);
    /**
     * 服务对象
     */

    @Resource
    private MemberRechargeLogService memberRechargeLogService;

    @Resource
    private MemberService memberService;

    /**
     * @Description: 用户充值次数及充值金额
     * @Param: [token, operatorMemberSn, params]
     **/
    @PostMapping("/memberRechargeMsg")
    public Result memberRechargeMsg(@RequestHeader("token")String token,
                                    @NotEmpty(message="memberSn 不能为空")String memberSn){
        //充值次数及充值金额
        LinkedHashMap<String, Object> memberRechargeMsg = memberService.findMemberRechargeMsg(memberSn,null);
        return ResultFactory.buildSuccessResult(memberRechargeMsg);
    }
    /**
     * @Description: 用户充值列表
     * @Param: [token, operatorMemberSn, params]
     **/
    @PostMapping("/memberRechargeList")
    public Result memberRechargeList(@RequestHeader("token")String token,
                                     @RequestParam Map<String, String> params){
        String memberSn = params.get("memberSn");
        String page = params.get("page");
        String limit = params.get("limit");
        //用户充值列表
        List<LinkedHashMap<String, Object>> memberRechargeList = memberService.findMemberRechargeList(null,null,memberSn,Integer.parseInt(page),Integer.parseInt(limit));
        Integer memberRechargeListCount = memberService.findMemberRechargeListCount(null,null, memberSn);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count",memberRechargeListCount);
        jsonObject.put("list",memberRechargeList);
        return ResultFactory.buildSuccessResult(jsonObject);
    }
    /**
     * @Description: 用户充值单号详情
     * @Param: [token, operatorMemberSn, params]
     **/
    @PostMapping("/rechargeLog")
    public Result refundLog(@RequestHeader("token")String token,
                            @RequestParam Map<String, String> params){
        String rechargeSn = params.get("rechargeSn");
        //用户充值单号详情 LinkedHashMap<String, Object> refundLog = memberRechargeService.findRefundLogBySn(rechargeSn);
        LinkedHashMap<String, Object> rechargeLog = memberRechargeLogService.findRechargeLogBySn(rechargeSn);
        if(null ==rechargeLog ||rechargeLog.isEmpty()){

        }else{
            rechargeLog.put("member_balance", CommonUtil.formatMoney(rechargeLog.get("member_balance"),2));
            rechargeLog.put("amount", CommonUtil.formatMoney(rechargeLog.get("amount"),2));
            rechargeLog.put("actual_amount", CommonUtil.formatMoney(rechargeLog.get("actual_amount"),2));
            rechargeLog.put("discount_amount", CommonUtil.formatMoney(rechargeLog.get("discount_amount"),2));
        }
        return ResultFactory.buildSuccessResult(rechargeLog);
    }
}
